package com.gcloud.persistance;

import java.sql.SQLException;
import java.util.ArrayList;

import com.gcloud.dto.GCloudFile;
import com.utils.Constants;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class SQLiteHandler extends SQLiteOpenHelper {

	public SQLiteHandler(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	/**
	 * Create the index table and account table
	 */

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		arg0.execSQL(Querys.createAccountTable);
		arg0.execSQL(Querys.createTableIndex);
	}

	/**
	 * Method not implemented, GCloud won't to drop its tables
	 */

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	/**
	 * Open new connection
	 */

	public void openConnection() {
		this.getWritableDatabase();
	}

	/**
	 * Close actual connection
	 */

	public void closeConnection() {
		this.close();
	}

	/**
	 * Method use to insert a new file in index table
	 * 
	 * @param key
	 * @param name
	 * @param description
	 * @param mimeType
	 * @param account
	 * @param uri
	 * @param isDirectory
	 * @param size
	 * @param service
	 * @param isFavorite
	 */

	public void insertFile(String key, String name, String description,
			String mimeType, String account, String uri, boolean isDirectory,
			long size, int service, boolean isFavorite) {
		ContentValues fileProperties = new ContentValues();
		fileProperties.put(Constants.FILE_KEY, key);
		fileProperties.put(Constants.FILE_NAME, name);
		fileProperties.put(Constants.FILE_DESCRIPTION, description);
		fileProperties.put(Constants.FILE_MIMETYPE, mimeType);
		fileProperties.put(Constants.FILE_ACCOUNT, account);
		fileProperties.put(Constants.FILE_URL, uri);
		fileProperties.put(Constants.FILE_SIZE, size);
		fileProperties.put(Constants.FILE_IS_DIRECTORY, isDirectory);
		fileProperties.put(Constants.FILE_SERVICE, service);
		fileProperties.put(Constants.FILE_FAVORITE, isFavorite);
		this.getWritableDatabase().insert(Constants.INDEX_FILE_SERVICE, null,
				fileProperties);
		closeConnection();
	}

	/**
	 * Check as favorite a file
	 * 
	 * @param key
	 */

	public void checkAsFavorite(String key) {
		checkAsFavorite(key, 1);
	}

	/**
	 * Uncheck as favorite a file
	 * 
	 * @param key
	 */
	public void uncheckAsFavorite(String key) {
		checkAsFavorite(key, 0);
	}

	private void checkAsFavorite(String key, int value) {
		ContentValues fileNewValue = new ContentValues();
		fileNewValue.put(Constants.FILE_FAVORITE, value);
		this.getWritableDatabase()
				.update(Constants.INDEX_FILE_SERVICE, fileNewValue,
						Constants.FILE_KEY + " = ?", new String[] { key });
		closeConnection();
	}

	/**
	 * Delete a file from local database specifying the key
	 * 
	 * @param key
	 */

	public void deleteFile(String key) {
		this.getWritableDatabase().delete(Constants.INDEX_FILE_SERVICE,
				Constants.FILE_KEY + " = ?", new String[] { key });
		closeConnection();
	}

	/**
	 * Return a select in disorder
	 * 
	 * @return <b>List<GCloudFile></b>
	 * @throws SQLException
	 */

	public ArrayList<GCloudFile> getOrderDetails() throws SQLException {

		ArrayList<GCloudFile> fileList = new ArrayList<GCloudFile>();
		Cursor cursor =

		this.getWritableDatabase().query(
				true,
				Constants.INDEX_FILE_SERVICE,
				new String[] { Constants.FILE_KEY, Constants.FILE_ACCOUNT,
						Constants.FILE_DESCRIPTION, Constants.FILE_FAVORITE,
						Constants.FILE_ID, Constants.FILE_IS_DIRECTORY,
						Constants.FILE_MIMETYPE, Constants.FILE_SERVICE,
						Constants.FILE_SIZE, Constants.FILE_URL,
						Constants.FILE_NAME, }, null, null, null, null,
				Constants.FILE_NAME, null);

		if (cursor.moveToFirst()) {
			do {
				GCloudFile gCloudFile = new GCloudFile();
				gCloudFile.setAccount(cursor.getString(cursor
						.getColumnIndexOrThrow(Constants.FILE_ACCOUNT)));
				gCloudFile.setDescription(cursor.getString(cursor
						.getColumnIndexOrThrow(Constants.FILE_DESCRIPTION)));
				gCloudFile
						.setDirectory(cursor.getInt((cursor
								.getColumnIndexOrThrow(Constants.FILE_IS_DIRECTORY))) == 1 ? true
								: false);
				gCloudFile.setKey(cursor.getString(cursor
						.getColumnIndexOrThrow(Constants.FILE_KEY)));
				gCloudFile.setMimeType(cursor.getString(cursor
						.getColumnIndexOrThrow(Constants.FILE_MIMETYPE)));
				gCloudFile.setName(cursor.getString(cursor
						.getColumnIndexOrThrow(Constants.FILE_NAME)));
				gCloudFile.setSize(cursor.getLong(cursor
						.getColumnIndexOrThrow(Constants.FILE_SIZE)));
				gCloudFile.setUri(Uri.parse(cursor.getString(cursor
						.getColumnIndexOrThrow(Constants.FILE_URL))));
				fileList.add(gCloudFile);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return fileList;
	}

}
